import { get, post, put } from '../../common/ajax';
import { BRD_ACTIVITY_URL, USR_LOTTERY_URL } from '../../constants/url';
import { dealAwardRecordListData} from '../../biztools/record';
import { dealActivityDetailData } from '../../biztools/activity';
import { dealLotterySaveData, dealAwardReporterListData } from '../biztools/lottery';

/**
 * 创建抽奖活动
 * @param {Object} params 活动参数
 */
export const editLotteryActivity = async (params) => {
    const { brandId, activityId } = params;
    params = dealLotterySaveData(params);
    const res = activityId ? await put(BRD_ACTIVITY_URL.update(activityId, brandId), params) : await post(BRD_ACTIVITY_URL.create(), params);
    return res.data || false;
};

/**
 * 查看抽奖活动信息
 */
export const getLotteryActivity = async (params) => {
    const { brandId, activityId, isCopy = false } = params;
    const res = await get(BRD_ACTIVITY_URL.detail(activityId, brandId));
    return res.data ? dealActivityDetailData(res.data, isCopy) : {};
};

/**
 * 获取抽奖活动详情
 * @param {String|Number} activityId 抽奖活动id
 */
export const getLotteryActivityDetail = async (activityId) => {
    const res = await get(USR_LOTTERY_URL.lotteryInfo(activityId));
    const { def = {}, procedure = {} } = res.data || {};
    return {
        lotteryInfo: dealActivityDetailData(def),
        procedureInfo: procedure,
    };
};

/**
 * 执行抽奖
 * @param {Object} params 抽奖参数
 */
export const doInvokeLottery = async (params) => {
    const res = await post(USR_LOTTERY_URL.lotteryDo, params);
    return res.data || false;
};

/**
 * 获取抽奖活动中奖人
 * @param {String|Number} activityId 抽奖活动id
 */
export const getAwardReporterList = async (activityId) => {
    const params = { pageNo: 1, pageSize: 100, activityId };
    const res = await get(USR_LOTTERY_URL.reporterAwards, params);
    return res.data ? dealAwardReporterListData(res.data.rows) : [];
};

/**
 * 获取中奖记录列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
 export const getAwawrdList = async (params) => {
    params.pageSize = params.pageSize || 10;
    params.awardType = 'real_prize';
    const res = await get(USR_LOTTERY_URL.lotteryAwards, params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealAwardRecordListData(rows) };
};
